package com.develop.collection.middle.sensors;

import com.develop.collection.util.Entry;
import com.develop.jdbc.jdbc.DataRow;
import com.develop.jdbc.jdbc.session.Session;
import com.develop.jdbc.jdbc.session.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 评分计算
 *
 * @author fenny
 * @since 2019/08/19
 */
public class SyncUser {

    private static final Logger log = LoggerFactory.getLogger(SyncUser.class);

    private static final AtomicInteger SUCCESS_COUNT = new AtomicInteger();

    private static final AtomicInteger FILTER_COUNT = new AtomicInteger();

    private static final AtomicInteger CONVERT_COUNT = new AtomicInteger();

    /**
     * 获取所有用户
     * 计算每个用户对应产品的评分
     * 写入txt
     */
    private static void execute() {
        log.info("begin-计算");
        int currTotal = 0;
        int pageSize = 5000;
        int i = 1;
        long beginTime;
        ExecutorService pool = Executors.newFixedThreadPool(1000);
        String fileName = "recommendations.txt";
        try {
            while (true) {
                Session mysql = SessionFactory.getSession("local-mysql");
                int offset = (i - 1) * pageSize;
                beginTime = System.currentTimeMillis();
                log.info("开始计算_{}_页-每页_{}_条", i, pageSize);
                List<DataRow> result = mysql.query(String.format("select distinct user_id from summary limit %s,%s", offset, pageSize));
                mysql.close();
                if (result.size() > 0) {
                    i++;
                    CountDownLatch latch = new CountDownLatch(result.size());
                    currTotal += result.size();
                    for (DataRow row : result) {
                        pool.submit(() -> {
                            Session newMysql = null;
                            try {
                                long userId = row.getLong("user_id");
                                newMysql = SessionFactory.getSession("local-mysql");
                                DataRow dataRow = new DataRow();
                                dataRow.set("user_id", userId);
                                newMysql.insert("user", dataRow);
                                SUCCESS_COUNT.incrementAndGet();
                            } catch (Exception e) {
                                log.error("", e);
                            } finally {
                                latch.countDown();
                                if (newMysql != null) {
                                    newMysql.close();
                                }
                            }
                        });
                    }
                    latch.await();
                } else {
                    break;
                }
                log.info("耗时{}秒,写入={}", ((System.currentTimeMillis() - beginTime) / 1000), SUCCESS_COUNT.get());
            }
        } catch (Exception e) {
            log.error("", e);
        } finally {
            pool.shutdown();
            log.info("原数据量={},过滤量={},写入量={},转换量={}", currTotal, FILTER_COUNT.get(), SUCCESS_COUNT.get(), CONVERT_COUNT.get());
            log.info("end-计算");
        }
    }

    public static void main(String[] args) {
        Entry.entry(SyncUser::execute);
    }

    private static void append(String fileName, String content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.write("\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
